/**
 * Created by zycool on 2016/9/6.
 * 运行计划(日历) - 阴保运行管理 - 腐蚀控制
 */

;(function($,window,document,undefined){

    // 管道
    var $pipeline = $('#JQ_pipeline').select3();
    // 管段
    var $segment = $('#JQ_segment').select3({
        title:'管段'
    });
    // 起点位置
    var $begin = $('#JQ_begin').select3({
        title: '起点位置',
        server: true,
        param: {'pipeLineId':$pipeline,'pipeSegmentId':$segment}
    });
    // 终点位置
    var $end = $('#JQ_end').select3({
        title:'终点位置',
        server: true,
        param: {'pipeLineId':$pipeline,'pipeSegmentId':$segment,'beginSignId':$begin}
    });
    // 触发管道
    $pipeline.element.on('select',function(e,v){
        var id = v || $pipeline.val();
        if(id){
            // 管段
            DyyBase.ajax(DyyBase.url_segment(), {pipeLineId:id}, function(dataset){
                var json = DyyBase.getSelect(dataset);
                $segment.add(json);
            });
            // 起点 & 终点
            DyyBase.ajax(DyyBase.url_pipe_pos(), {pipeLineId:id}, function(dataset){
                var begin = DyyBase.getPos(dataset,'begin');
                var end = DyyBase.getPos(dataset,'end');
                $begin.asset(begin);
                $end.asset(end);
            });
        }else{
            $segment.add([]);
            $begin.add([]);
            $end.add([]);
        }
    });
    // 触发管段
    $segment.element.on('select',function(e,v){
        var id = v || $segment.val();
        if(id){
            // 起点 & 终点
            DyyBase.ajax(DyyBase.url_segment_pos(), {pipeSegmentId:id}, function(dataset){
                var begin = DyyBase.getPos(dataset,'begin');
                var end = DyyBase.getPos(dataset,'end');
                $begin.asset(begin);
                $end.asset(end);
            });
        }else{
            $begin.add([]);
            $end.add([]);
        }
    });

    // 查询参数
    var filter = {
        getSearch: function(){
            var data = {};
            data.pipeLineId = function(){
                return $pipeline.val();
            }();
            data.fromId = function(){
                return $begin.val();
            }();
            data.toId = function(){
                return $end.val();
            }();
            data.showValveRoom = function(){
                if($('#JQ_room_check').hasClass('active')){
                    return 'true';
                }else{
                    return 'false';
                }
            }();
            data.dateStr = function(){
                var year = $calender.data('year');
                var month = $calender.data('month');
                return year + '-' + month;
            }();
            return data;
        }
    };

    // 初始日历
    var $calender = $('#JQ_plan_calender').CLD();
    $calender.on('change',function(){
        addShow();
    });

    // 点击事件
    $calender.on('click','td[data-id]',function(){
        // 附加数据
        var $this = $(this);
        var data = $this.data('store');
        if(data){
            var $td = $calender.find('td[data-id]');
            $td.removeClass('select');
            $this.addClass('select');

            showFlex(data);
        }
    });


    // 显示设备对象
    function showFlex(data){
        var $flex = $('#JQ_plan_flex').empty();
        var i = 0;
        var box = '';
        for(var k in data){
            i++;
            box += '<div class="zy-plan-div">'
                + '<div class="zy-plan-meta">'
                + '<i class="zy-plan-num">'+i+'</i>'
                + '<span class="f16 fwb">'+k+'</span>'
                + '</div>'
                + '<div class="zy-plan-body">'
                + '<p><span class="c-90">测试对象：</span>'+k+'</p>'
                + '<p><span class="c-90">测试项目：</span>'+data[k]+'</p>'
                + '</div></div>';
        }
        if((i%3) === 0){
            $flex.append(box);
        }else{
            //box += '<div class="zy-plan-div"></div>';
            $flex.append(box);
        }
    }

    // 添加日历事件
    function addShow(){
        $('#JQ_plan_flex').empty();
        var param = filter.getSearch();
        DyyBase.ajax(corrctrlApi.url_plan_search_calendar(), param, function(dataset){
            var $td = $calender.find('td.current');
            $td.each(function(){
                var $this = $(this);
                $this.removeClass('yes');
                $this.removeClass('select');
                $this.find('span').remove();
            });
            if(dataset.bool){
                var data = dataset.result;
                if($.isEmptyObject(data)){
                    $td.each(function(){
                        var $this = $(this);
                        $this.data('store','');
                    });
                }else{
                    $td.each(function(){
                        var $this = $(this);
                        var id = $this.attr('data-id');
                        if(data.hasOwnProperty(id)){
                            for(var k in data[id]){
                                var span = '<span class="zy-current-td-span">'+k+'</span>';
                                $this.addClass('yes');
                                $this.find('span.zy-current-td-span').remove();
                                $this.append(span).data('store',data[id]);
                                break;
                            }
                        }
                    });
                }
            }
        });
    }

    // 查询
    $('#JQ_search_calendar').click(function(){
        addShow();
    });
    $('#JQ_search_calendar').trigger('click');


})(jQuery,window,document);